<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>

<c:set var="ctx" value="${pageContext.request.contextPath}" />
<!DOCTYPE HTML>
<html>
<script>
	var ctx = "${ctx}";
</script>
<head>
<title>支付</title>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link href="${ctx}/static/css/bootstrap.min.css?v=${static_version}"
	rel="stylesheet" />
<link href="${ctx}/static/css/animate.min.css?v=${static_version}"
	rel="stylesheet" />
<link href="${ctx}/static/css/style.min.css?v=${static_version}"
	rel="stylesheet" />
<link href="${ctx}/static/css/common.css?v=${static_version}"
	rel="stylesheet" />
<link href="//at.alicdn.com/t/font_2tga2cdp538lg14i.css"
	rel="stylesheet" />
</head>
<body class="gray-bg">
	<div class="wrapper wrapper-content  animated fadeInRight">
		<div class="ibox float-e-margins">
			<!-- 订单支付 -->
			<div class="ibox-title">
				<h5>
					订单支付<small></small>
				</h5>
			</div>
			<div class="ibox-content">

				<input type="hidden" id="subject" value="${order.titles}" /> <input
					type="hidden" id="order_money" value="${order.money*100}" /> <input
					type="hidden" id="order_num" value="${order.ordernum}" /> <input
					type="hidden" id="order_id" value="${order.rid}" /> <input
					type="hidden" id="return_url" value="${return_url}" />

				<div class="form-group lineheight30 clear">
					<label class="col-sm-2 control-label">您的订单号</label>
					<div class="col-sm-10">${order.ordernum}</div>
				</div>
				<div class="form-group lineheight30 clear">
					<label class="col-sm-2 control-label">订单待支付金额</label>

					<div class="col-sm-10">${order.money}元</div>
				</div>
				<div class="hr-line-dashed2"></div>
				<div class="lineheight30 clear">
					<label class="col-sm-2 control-label">支付方式</label>
				</div>
				<div class="hr-line-dashed2"></div>

				<input type="hidden" id="pay_type" value="1" />
				<div class="mt-10 form-group lineheight30 clear">
					<div class="col-sm-10">
						<div class="col-sm-2">
							<input type="radio" checked="checked" value="1" id="pay_type_1"
								name="pay_type">&nbsp;&nbsp;&nbsp;&nbsp;<i
								class="icon iconfont icon-yuezhifu color-primay font-size-30"></i>
						</div>
						<span>首媒支付 &nbsp;&nbsp;&nbsp;&nbsp; 用户余额：${user.c_money}</span>
					</div>
					<div id="div_moneypay" class="col-sm-10">
						<span class="col-sm-2">支付密码 </span> <span
							class="col-sm-2 m-l-n mb-10"><input class="form-control"
							type="password" name="paypwd" id="paypwd"></span>
					</div>
					<div class="col-sm-10">
						<div class="col-sm-2">
							<input type="radio" value="2" id="pay_type_2" name="pay_type">&nbsp;&nbsp;&nbsp;&nbsp;<i
								class="icon iconfont icon-zhifubaozhifu color-primay font-size-30"></i>
						</div>
						<span
							onclick="topay(${order.money*100},${order.ordernum},'alipay_pc_direct')">支付宝</span>
					</div>
					<div class="col-sm-10">
						<div class="col-sm-2">
							<input type="radio" value="3" id="pay_type_3" name="pay_type">&nbsp;&nbsp;&nbsp;&nbsp;<i
								class="icon iconfont icon-weixinzhifu color-primay font-size-30"></i>
						</div>
						<span
							onclick="topay(${order.money*100},${order.ordernum},'wx_pub_qr')">微信</span>
						<div id="div_qrcode" style="display: none;">
							扫描微信支付二维码：
							<div id="qrcode"></div>
						</div>
					</div>
				</div>
				<div class="form-group lineheight30 clear">
					<div class="col-sm-10">
						<button type="button" class="btn btn-primary col-sm-2"
							onclick="dopay()">确认支付</button>
					</div>
				</div>

			</div>
			<!-- /ibox-content -->
		</div>
	</div>

	<script src="${ctx}/static/js/jquery.min.js?v=${static_version}"></script>
	<script src="${ctx}/static/js/jquery.qrcode.min.js"></script>
	<script src="${ctx}/static/js/bootstrap.min.js?v=${static_version}"></script>

	<script src="${ctx}/static/js/pingpp.js"></script>
	<script type="text/javascript">
	
	var paytype = 1;
	var order_money,order_id;
	var order_num;
	var return_url;
	
	$(function(){ 
		return_url = $("#return_url").val();
		/*选择支付方式*/
		$("#pay_type_1,#pay_type_2,#pay_type_3").change(function() { 				
			paytype = $("input[name='pay_type']:checked").val();
			$("#pay_type").val(paytype);
			if(paytype==2 || paytype==3){
				$("#div_moneypay").hide();
			}else{
				$("#div_moneypay").show();
			}
		}); 
		/*/选择支付方式*/
	});
	//加载数据

	function usermoneypay(order_num,order_money) {
		if ($("#paypwd").val() == '') {
			parent.layer.alert("密码不能为空！", {
				icon : 2,
				skin : 'layer-ext-moon'
			})
			return;
		}
		$.ajax({
			type : "post",
			url : "${ctx}/usermoneypay",
			data : "order_num=" + order_num + "&paypwd=" + $("#paypwd").val()+"&order_money="+order_money,
			success : function(msg) {
				var info = eval('(' + msg + ')');
				if (info.error == '0000') {
					parent.layer.alert('支付成功', {
						icon : 1,
						skin : 'layer-ext-moon'
					})
					window.location.href = return_url;
				} else {
					parent.layer.alert(info.information, {
						icon : 2,
						skin : 'layer-ext-moon'
					})
				}
			}
		})
	}
	
	/*支付*/
	function dopay (){
		order_money = $("#order_money").val();
		order_num = $("#order_num").val();
		order_id = $("#order_id").val();
		if(paytype==1){
			usermoneypay(order_num,order_money);			
		}
		if(paytype==2){
			$("#div_moneypay").hide();
			topay(order_money,order_num,'alipay_pc_direct');
		}
		if(paytype==3){
			$("#div_moneypay").hide();
			topay(order_money,order_num,'wx_pub_qr');
		}
	}
	// 通过订单获取ping++支付对象
	function topay(money,ordernum,channel){				
		$.ajax({
			type : "post",
			url : "${ctx}/getPingppCharge",
			data : "amount=" + money + "&channel=" + channel + "&client_ip=127.0.0.1&subject=" + $("#subject").val() + "&order_desc=" + $("#subject").val() + "&order_no=" + ordernum,
			success : function(msg) {
				if (msg !=null) {
					if(msg.channel=="alipay_pc_direct"){
						pingpp.createPayment(msg, function(
							result, err) {
							console.log(result);
							console.log(err.msg);
							console.log(err.extra);
							if (result == "success") {// 只有微信公众账号 wx_pub 支付成功的结果会在这里返回，其他的支付结果都会跳转到 extra 中对应的 URL。

							} else if (result == "fail") {	// charge 不正确或者微信公众账号支付失败时会在此处返回

							} else if (result == "cancel") {// 微信公众账号支付取消支付

							}
						});
					}else{
						var qrcodeurl = msg.credential.wx_pub_qr;
						//alert(qrcodeurl);
						$("#div_qrcode").show();
						$('#qrcode').qrcode({width: 100,height: 100,text: qrcodeurl});
					}						
				}
			}
		})
	}
	</script>
</body>
</html>